package com.spring.framework.config.event.scheduled;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.LockSupport;

/**
 * 三个调度任务
 *
 * @author xuweizhi
 */
@Configuration
@EnableScheduling
@ComponentScan(basePackages = "com.spring.framework.config.event.scheduled")
public class AnnScheduleMethodConfig {

	public final static LongAdder SCHEDULED_COUNT = new LongAdder();

	@Scheduled(fixedDelay = 2000)
	public void Scheduled1() {
		SCHEDULED_COUNT.increment();
		System.out.println("-----Scheduled1：" + Thread.currentThread().getName());
		//执行3秒
		LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(3));
	}

	@Scheduled(fixedRate = 1000)
	public void Scheduled2() {
		SCHEDULED_COUNT.increment();
		System.out.println("-----Scheduled2：" + Thread.currentThread().getName());
		//执行1秒
		LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1));
	}

	@Schedules({@Scheduled(fixedRate = 1000), @Scheduled(fixedDelay = 2000)})
	//@Scheduled(cron = "*/5 * * * * MON-FRI")
	public void Scheduled3() {
		SCHEDULED_COUNT.increment();
		System.out.println("-----Scheduled3：" + Thread.currentThread().getName());
		//执行2秒
		LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));
	}
}
